package com.gamemost.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {
	public static String DOWNLOAD_BOOK = "download_book";
	private static final String DATABASE_NAME = "readerepub";
	public static final int DATABASE_VERSION = 1;
	private static String DB_PATH = "/data/data/com.gamemost.lifestory/databases/";
	private static String DB_NAME = "readerepub";
	public static SQLiteDatabase database;
	private final Context myContext;
	public static MySQLiteHelper instance;

	public static MySQLiteHelper getInstance(Context context) {
		if (instance == null) {
			System.out.println("init mysql");

			instance = new MySQLiteHelper(context);
		}
		return instance;
	}

	private static final String DATABASE_CREATE = "create table "
			+ DOWNLOAD_BOOK + "(" + BookDatasource.TB_ID
			+ " integer primary key autoincrement, " + BookDatasource.BOOK_ID
			+ " text not null, " + BookDatasource.BOOK_FILE_NAME
			+ " text not null, " + BookDatasource.BOOK_IMAGE
			+ " text not null, " + BookDatasource.BOOK_BOOKMARK
			+ " text not null, " + BookDatasource.CATEGORY_ID
			+ " text not null," + BookDatasource.TITLE + " text not null,"
			+ BookDatasource.URL + " text not null,"
			+ BookDatasource.TAG_BOOKCOVER + " text not null,"
			+ BookDatasource.BOOK_TYPE + " text not null);";

	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.myContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		// TODO Auto-generated method stub
		database.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.w(MySQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		onCreate(db);
	}

	public void update() {
		String myPath = DB_PATH + DB_NAME;
		SQLiteDatabase db = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READONLY);
		int version = db.getVersion();
		onUpgrade(db, version, DATABASE_VERSION);

	}

	public void createDataBase() throws IOException {

		boolean dbExist = checkDataBase();
		if (dbExist) {

		} else {
			this.getReadableDatabase();

		}

	}

	private boolean checkDataBase() {

		SQLiteDatabase checkDB = null;

		try {
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null,
					SQLiteDatabase.OPEN_READONLY);

		} catch (SQLiteException e) {

		}

		if (checkDB != null) {

			checkDB.close();

		}

		return checkDB != null ? true : false;
	}

	public SQLiteDatabase openDataBase() throws SQLException {
		String myPath = DB_PATH + DB_NAME;
		database = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READWRITE);
		return database;
	}

	@Override
	public synchronized void close() {

		if (database != null)
			database.close();

		super.close();

	}
}
